package com.xxwu.springlibrary.controller;

import com.xxwu.springlibrary.contans.Contans;
import com.xxwu.springlibrary.model.UserInfo;
import com.xxwu.springlibrary.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/userController")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public boolean login(String username, String password , HttpSession session) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        //验证密码，根据输入的用户名去用户表中查找对应的数据并返回
        UserInfo userInfo = userService.getUserInfo(username);
        //若没有找到对应用户
        if(userInfo == null) {
            return false;
        }
        //账号密码正确
        if(password.equals(userInfo.getPassword())) {
            session.setAttribute(Contans.USER_SESSION_KEY, userInfo);
            return true;
        }
        return false;
    }
}
